﻿Imports System.Windows.Forms

Public Class frSettings

    ''' <summary>
    ''' Läd Einstellungen aus My.Settings und Registry
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub loadSettings()
        txUsername.Text = My.Settings.GaysUsername
        txPassword.Text = (My.Settings.GaysPassword)
        ckSoundNotification.Checked = My.Settings.SoundNotification
        nbReload.Value = CDec(My.Settings.RefreshPeriod)

        ckAutostart.Checked = notifierFunctions.autostartEnabled
    End Sub

    ''' <summary>
    ''' Speichert Einstellungen in My.Settings und Registry
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub saveSettings()
        Try

            My.Settings.GaysUsername = txUsername.Text
            My.Settings.GaysPassword = (txPassword.Text)
            My.Settings.SoundNotification = ckSoundNotification.Checked
            My.Settings.RefreshPeriod = CInt(nbReload.Value)

            My.Settings.Save()

            notifierFunctions.autostartEnabled = ckAutostart.Checked
        Catch ex As Exception
            MsgBox("Beim Speichern der Einstellungen ist ein Fehler aufgetreten:" + vbCrLf + ex.Message, MsgBoxStyle.Critical, "Fehler")
        End Try

    End Sub


#Region "Dialog Events"

    ''' <summary>
    ''' Prüft Einstellungen, speichert, schliesst Fenster
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim g As New gaysComm(txUsername.Text, txPassword.Text)

        Try
            Me.Cursor = Cursors.WaitCursor

            If g.checkCredentials Then
                saveSettings()

                Me.DialogResult = System.Windows.Forms.DialogResult.OK
                Me.Close()
            Else
                MsgBox("Diese Zugangsdaten sind nicht gültig", MsgBoxStyle.Critical, "Zugangsdaten prüfen")
            End If

            Me.Cursor = Cursors.Default

        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox("Beim Speichern der Einstellungen ist ein Fehler aufgetreten: " + vbCrLf + vbCrLf + ex.Message)

            'TODO Logging

        End Try

    End Sub

    ''' <summary>
    ''' Nichts speichern, nur abbrechen
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

#End Region

#Region "Form Events"

    ''' <summary>
    ''' Form Iniitialisierung: Einstellungen laden
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub frSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadSettings()
    End Sub

#End Region

End Class
